/*
Another game of life with SDL renderer.
Copyright (C) 2009-2010  Julien CLEMENT (andematunum@gmail.com)
*/
/*
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "SDLMatrixRenderer.h"
#include "Matrix.h"
#include "Cell.h"

SDLMatrixRenderer::
SDLMatrixRenderer (Matrix *matrix, Uint16 w, Uint16 h, Uint8 r, Uint8 g, Uint8 b) :
				MatrixRenderer(matrix,w,h,r,g,b) {
}

void
SDLMatrixRenderer::
render (void) {
	static SDL_Surface * screen = SDL_GetVideoSurface ();
	
	SDL_Rect rect;
	unsigned int r, g, b;
	Uint32 color;

	Cell ** cells = _matrix->getData();

	rect.w = _w;
	rect.h = _h;

	for (int row=0; row < _matrix->_h; row++) {
		for (int c=0; c < _matrix->_w; c++) {
			Cell cell = cells[row][c];
	
			rect.x = c *   (rect.w+1);
			rect.y = row * (rect.h+1);

			if (cell.alive()) {
				if (_matrix->_usecolors) {
					cell.getColor(r,g,b);
					color = SDL_MapRGB(screen->format, (Uint8)r, (Uint8)g, (Uint8)b);
				}
				else {
					color = SDL_MapRGB(screen->format, _r,_g,_b);
				}
			}
			else {
				color = SDL_MapRGB(screen->format, 0,0,0);
			}
			SDL_FillRect (screen, &rect, color);
		}
	}

	SDL_UpdateRect(screen, 0, 0, _matrix->_w * (_w+1), _matrix->_h * (_h+1));
}

